package com.anteater.eattheant;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.anteater.eattheant.util.Level;
import com.anteater.eattheant.util.Ranking;
import com.anteater.eattheant.util.SoundPlayer;
import com.anteater.eattheant.view.ETASurfaceView;
import com.anteater.eattheant.view.ETASurfaceView.EndListener;
import com.anteater.eattheant.view.ETASurfaceView.SoundListener;

public class PlayActivity extends Activity {
	private Ranking ranking;
	private ETASurfaceView surfaceView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		ranking = new Ranking(this);
		Integer levelNum = null;
		if (getIntent().getExtras() != null
				&& !getIntent().getExtras().isEmpty()) {
			levelNum = getIntent().getExtras().getInt("level");
		} else {
			levelNum = ranking.getMaxLevel();
		}

		Level level = null;
		if (levelNum == null) {
			level = new Level(this, 0);
		} else {
			if (levelNum >= LevelActivity.NUMBER_LEVELS-1) {
				level = new Level(this, levelNum);
			} else {
				level = new Level(this, levelNum + 1);
			}
		}

		final short min = level.getScore();
		final int levelPlayed = level.getLevel();

		setContentView(R.layout.play);

		Toast toast = Toast.makeText(this, level.getTitle() + "\n" + level.getDescription(), Toast.LENGTH_SHORT+10);
		toast.setGravity(Gravity.TOP, 0, 0);
		toast.show();

		surfaceView = (ETASurfaceView) findViewById(R.id.etasurfaceview);

		surfaceView.start(level, new EndListener() {
			@Override
			public void notify(short score) {
				score(levelPlayed, score, min);
			}
		}, new PlaySoundListener());

		setVolumeControlStream(AudioManager.STREAM_MUSIC);
	}

	@Override
	protected void onPause() {
		super.onPause();
		surfaceView.onPause();
		SoundPlayer.getInstance().pauseSounds();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		SoundPlayer.getInstance().pauseSounds();
		SoundPlayer.getInstance().playInBackground(this, R.raw.play_sound);
	}

	@Override
	protected void onResume() {
		super.onResume();
		SoundPlayer.getInstance().playInBackground(this, R.raw.play_sound);
		surfaceView.onResume();
	}

	private void score(int level, short score, short min) {
		Intent intent = new Intent(getBaseContext(), ScoreActivity.class);
		intent.putExtra("level", level);
		intent.putExtra("score", score);
		intent.putExtra("min", min);
		startActivity(intent);
		finish();
	}

	private class PlaySoundListener implements SoundListener {

		@Override
		public void stopTongueSound() {
			SoundPlayer.getInstance().playSound(getBaseContext(),
					R.raw.final_lingua);
		}

		@Override
		public void playCatchAnt() {
			SoundPlayer.getInstance().quickSound(getBaseContext(),
					R.raw.captura_formiga);
		}

		@Override
		public void playTonqueSound() {
			SoundPlayer.getInstance()
					.playNext(getBaseContext(), R.raw.inicio_lingua,
							R.raw.som_lingua_extendido, true, true);
		}
	}
}